//
//  AXFLocationSearchViewController.m
//  AXFEightGroup
//
//  Created by Haozhong Chen on 2016/12/28.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFLocationSearchViewController.h"
#import "AXFLocationManager.h"
#import <objc/message.h>

static NSString*cellID = @"AXFLocationSearchViewControllerCellID";
//const NSString* kUpdateLocationManagerData = @"kUpdateLocationManagerData";

@interface AXFLocationSearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate, UIGestureRecognizerDelegate>


{
    BOOL shouldShowSearchResults;
}
@property(nonatomic,strong)UISearchController*searchController;
@property(nonatomic,weak)UITableView*tableView;

@end

@implementation AXFLocationSearchViewController




- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupTableView];
    [self setupSearchController];
    [self setupNotificationCenter];

}




-(void)setupNotificationCenter
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSearchView) name:@"kUpdateLocationManagerData" object:nil];
    
}

-(void)updateSearchView
{
    [self.tableView reloadData];
}
-(void)setupSearchController
{
    
    //searchResultsController
    _searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
    
    _searchController.searchResultsUpdater=self;

    _searchController.searchBar.placeholder=@"查看详细地址";
    _searchController.dimsBackgroundDuringPresentation=YES;
    _searchController.searchBar.delegate=self;
    self.tableView.tableHeaderView=_searchController.searchBar;
    [_searchController.searchBar sizeToFit];
    
    
}

-(void)setupTableView
{
    UITableView*tableView=[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView=tableView;
    [self.view addSubview:tableView];
    tableView.dataSource=self;
    tableView.delegate=self;
//    tableView.backgroundColor=[UIColor lightGrayColor];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
}


-(NSMutableArray<CLPlacemark*>*)placemarkArray
{
    if(_placemarkArray==nil)
    {
        _placemarkArray=[[NSMutableArray alloc]init];
    }
    return _placemarkArray;
}

#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    shouldShowSearchResults = YES;
    [self.tableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    shouldShowSearchResults = NO;
    [self.tableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if (!shouldShowSearchResults) {
        shouldShowSearchResults = YES;
        [self.tableView reloadData];
    }
    [self.searchController.searchBar resignFirstResponder];
}

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString * searchString = searchController.searchBar.text;
    NSPredicate * predicate = [NSPredicate  predicateWithFormat:@"SELF CONTAINS %@",searchString];
//    self.filteredArray = [[self.carNumberArray filteredArrayUsingPredicate:predicate] mutableCopy];
    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return shouldShowSearchResults ? self.filteredArray.count : self.carNumberArray.count;
    return [AXFLocationManager sharedInstance].placemmarkArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    CLPlacemark*model=[AXFLocationManager sharedInstance].placemmarkArray[indexPath.row];
    
    if (shouldShowSearchResults)
    {
        cell.textLabel.text = @"hehe";
    }else
    {
        cell.textLabel.text = model.name;
    }
//    cell.imageView.image = [UIImage imageNamed:@"car.png"];
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (shouldShowSearchResults)
    {
        
    }
    else
    {
        
        
    }
}

-(void)dealloc
{
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:kUpdateLocationManagerData object:nil];
}

@end
